<style type="text/css">
.tablesorter td, th
{
	text-align: center;
}
.tablesorter td
{
	/*width: 70px;*/
}
.tablesorter td.numeric_4
{
	text-align: right;
	/*width: 40px;*/
}
.tablesorter td.numeric, th.numeric
{
	text-align: right;
}
.tablesorter td.numeric_nozero, th.numeric_nozero
{
	text-align: right;
	/*width: 80px;*/
}
.tablesorter td.fund, th.fund
{
	text-align: center;
	/*width: 160px;*/
}
</style>
<?php
	/**
	 * Elgg jQuery Table Sorter plugin
	 * 
	 * @author  Lenny Urbanowski
	 * @link	http://www.itslennysfault.com
	 * @package ElggTableSorter
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 *
	 * Created for Pixel Brothers Interactive (http://www.pixbros.com)
	 */

	//load external JS if not already loaded
	global $tablesorter_js_loaded;
	if (!isset($tablesorter_js_loaded)) $tablesorter_js_loaded = false;
	if (!$tablesorter_js_loaded) {
		?><script type="text/javascript" src="<?php print $CONFIG->wwwroot; ?>mod/tablesorter/js/jquery.tablesorter.min.js"></script><?php	
	}
	
	//JS to activate this sortable table
	$tblCodeJS='<script type="text/javascript">$(document).ready(function(){ $("#'.$vars['tableid'].'").tablesorter(); } ); </script>';
	$tblColumnHeads = array();
	$tblCodeBody="<tbody>";
	
	//pre build table head array allows us to populate empty cells
	foreach($vars['value'] as $tblRows){
		foreach($tblRows as $tblHead => $tblCells){
			$tblColumnHeads[$tblHead]= $tblHead;
		}
	}
	
	//build table body code
	foreach($vars['value'] as $tblRows){
		$index = array_keys($vars['value'],$tblRows,true);
		$class = ($index[0]+1)%2 ? 'odd':'even';
		$tblCodeBody.="<tr class='$class'>";
		foreach($tblColumnHeads as $tblHead){
			if (is_numeric($tblRows[$tblHead]) && (
					$tblHead=='% Cost'
					|| $tblHead=='% Mkt' 
					)) {
				$tblCodeBody.="<td class='numeric_4'>".number_format($tblRows[$tblHead],2)."</td>";
				continue;
			}
			if (is_numeric($tblRows[$tblHead]) && (
					$tblHead=='Quantity'
					|| $tblHead=='% Num' 
					|| $tblHead=='Mkt Val' 
					|| $tblHead=='Val' 
					|| $tblHead=='PnL' 
					|| $tblHead=='Pos' 
					)) {
				$tblCodeBody.="<td class='numeric_nozero'>".number_format($tblRows[$tblHead],0)."</td>";
				continue;
			}
			if (is_numeric($tblRows[$tblHead]) && $tblHead!='Ticker') {
				$tblCodeBody.="<td class='numeric'>".number_format($tblRows[$tblHead],2)."</td>";
				continue;
			}
			if ($tblHead=='Fund') {
				$tblCodeBody.="<td class='fund'>".$tblRows[$tblHead]."</td>";
				continue;
			}
			if($tblRows[$tblHead]) //make empty cells if needed
				$tblCodeBody.="<td>".$tblRows[$tblHead]."</td>";
			else
				$tblCodeBody.="<td>&nbsp;</td>";			
		}
		$tblCodeBody.="</tr>";
	}
	$tblCodeBody.="</tbody>";	
	
	//create table head code
	$tblCodeHead='<table id="'.$vars['tableid'].'" class="tablesorter" style="width:100%;border-collapse:separate;min-width:666px"><thead><tr>';
	foreach($tblColumnHeads as $tblHead){
		/*if ((
				$tblHead=='Quantity'
				|| $tblHead=='%1 Num' 
				|| $tblHead=='Mkt Value' 
				|| $tblHead=='Assert Val' 
				|| $tblHead=='PnL' 
				)) {
			$tblCodeHead.="<th class='numeric_nozero'>".$tblHead."</th>";
			continue;
		}
		if ($tblHead=='Fund') {
			$tblCodeHead.="<th class='fund'>".$tblHead."</th>";
			continue;
		}*/
		$tblCodeHead.="<th>".$tblHead."</th>";
	}
	$tblCodeHead.='</tr></thead>';
	
	//table footer
	$tblCodeFoot="</table>";
	
	//output all the code
	echo $tblCodeHead . $tblCodeBody . $tblCodeFoot . $tblCodeJS;
?>